//
//  BIDCommunityDedicationDetailViewController.m
//  HJGJCommunity
//
//  Created by mal on 2017/5/23.
//  Copyright © 2017年 mal. All rights reserved.
//

#import "BIDCommunityDedicationDetailViewController.h"
#import "BIDCommunityDedicationDetailCell.h"
#import <SDWebImage/UIImageView+WebCache.h>

static NSString *detailURL = @"socialPublicAndroidInfo/check.action";

@interface BIDCommunityDedicationDetailViewController ()<UITableViewDelegate, UITableViewDataSource>
{
    NSMutableArray *_dataSourceArr;
    BOOL _bRegister;
}
@end

@implementation BIDCommunityDedicationDetailViewController
@synthesize communityDedicationID;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.view setBackgroundColor:[UIColor whiteColor]];
    _dataSourceArr = [NSMutableArray array];
    _tableView.allowsSelection = NO;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.tableFooterView = [[UIView alloc] init];
    [self loadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)loadData
{
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], detailURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"id=%@", communityDedicationID];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(NSDictionary *responseDic, NSString *errMsg){
        if(errMsg)
        {
            [self showAlertView:errMsg buttonTitle:ALERT_BTN_TITLE tag:0 functionName:nil];
        }
        else
        {
            CGSize screenSize = [UIScreen mainScreen].bounds.size;
            NSDictionary *dic = responseDic[@"socialPublicInfo"];
            NSString *strContent = dic[@"content"];
            _contentLabel.text = strContent;
            CGFloat height = [BIDGlobalMethods getHeightWithString:strContent font:[UIFont systemFontOfSize:15.f] constraintSize:CGSizeMake(screenSize.width-32, MAXFLOAT)];
            _heightForContentLabelConstraint.constant = height;
            UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenSize.width-32, height+45)];
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 15, CGRectGetWidth(headerView.frame), height)];
            label.numberOfLines = 0;
            [label setFont:[UIFont systemFontOfSize:15.f]];
            [label setTextColor:RGBACOLOR(50, 54, 59, 1.f)];
            [label setText:strContent];
            [headerView addSubview:label];
            _tableView.tableHeaderView = headerView;
            [_dataSourceArr addObjectsFromArray:dic[@"files"]];
            [_tableView reloadData];
        }
    }];
}

#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    static NSString *identifier = @"identifier";
    NSUInteger row = indexPath.row;
    if(!_bRegister)
    {
        _bRegister = YES;
        UINib *nib = [UINib nibWithNibName:@"BIDCommunityDedicationDetailCell" bundle:nil];
        [tableView registerNib:nib forCellReuseIdentifier:identifier];
    }
    BIDCommunityDedicationDetailCell *detailCell = [tableView dequeueReusableCellWithIdentifier:identifier];
    NSDictionary *dic = _dataSourceArr[row];
    NSString *strFilePath = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], dic[@"path"]];
    NSLog(@"%@\r\n", strFilePath);
    [detailCell.imgView setImageWithURL:strFilePath placeholderImage:[UIImage imageNamed:@"slide_placeholder.png"]];
    cell = detailCell;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 223.f;
}


@end
